package com.tybbank.Fragment.fragmentui;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.tybbank.Base.BaseFragment;
import com.tybbank.Bean.FHBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.yuyh.library.imgsel.utils.LogUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/9/12 0012.
 */

public class FHListFragment extends BaseFragment {
    public String mTitle;
   // @BindView(R.id.rl_RecyclerView_list)
   private  RecyclerView rv_recyclerView;
   // @BindView(R.id.ptr_scroll_view)
   private PullToRefreshScrollView ptrScrollView;
   // @BindView(R.id.rl_gone)
   private   RelativeLayout rlGone;
  //  Unbinder unbinder;

    private int page;
    private String sXmTitle;
    private int sXbiaoji;
    private List<FHBean.DataBean> list  = new ArrayList<>();;
    private buylistdapter adapte;

    public static FHListFragment getInstance(String title) {
        FHListFragment sf = new FHListFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_fwbuylist;
    }

    @Override
    protected void lazyLoad() {
        rv_recyclerView = findViewById(R.id.rl_RecyclerView_list);
        ptrScrollView = findViewById(R.id.ptr_scroll_view);
        rlGone = findViewById(R.id.rl_gone);
        sXmTitle = mTitle;

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rv_recyclerView.setLayoutManager(manager);
        rv_recyclerView.setHasFixedSize(true);//每个 高度一样
        //创建并设置adapter
        //创建并设置adapter
        page = 1;
        rv_recyclerView.setNestedScrollingEnabled(false);
        ptrScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        ptrScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page = 1;
                init(sXmTitle);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                init(sXmTitle);
            }
        });
        init(sXmTitle);
    }



    private void init(String sXmTitle) {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("page", page + "");
        parameters.put("classify", sXmTitle + "");
        parameters.put("type",   "4");
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        initdata(time, sign, sXmTitle);
    }


    private void initdata(String time, String sign, String sXmTitle) {
        LogUtils.e("TAGatatat" + page + "___" + sXmTitle);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(getActivity(), getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.money_log)
                .addParams("uuid", MyApplication.uuid)
                .addParams("page", page + "")
                .addParams("classify", sXmTitle)
                .addParams("type", "4")
                .addParams("token", MyApplication.token)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @SuppressLint("WrongConstant")
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        ptrScrollView.onRefreshComplete();
                        Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogUtils.e("TAG列表fragment" + response.toString());
                        dialog.dismiss();
                        ptrScrollView.onRefreshComplete();
                        postReponse(response);
                    }

                    public void postReponse(String json) {
                        try {
                            FHBean enterBean = new Gson().fromJson(json, new TypeToken<FHBean>() {
                            }.getType());
                            int a = enterBean.getCode();
                            if (a == 1011) {
                                FHBean<List<FHBean.DataBean>> enter = new Gson().fromJson(json, new TypeToken<FHBean<List<FHBean.DataBean>>>() {
                                }.getType());
                                if (page == 1) {
                                    list.clear();
                                    list = enter.getData();
                                }
                                if (page > 1) {
                                    list.addAll(enter.getData());
                                }
                                if (page == 1 && list.isEmpty()) {
                                    rlGone.setVisibility(View.VISIBLE);
                                   // ptrScrollView.setVisibility(View.GONE);
                                    ptrScrollView.setVisibility(View.GONE);
                                    return;
                                }
                                //商品
                                adapte = new buylistdapter();
                                rv_recyclerView.setAdapter(adapte);
                                adapte.notifyDataSetChanged();
                            } else if (a == 1004) {
                                Intent intent = new Intent(getActivity(), LoginActivity.class);
                                startActivity(intent);
                                return;
                            }
                        } catch (Exception e) {

                        }

                    }
                });

    }



    public class buylistdapter extends RecyclerView.Adapter {


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.mill_adapter_item, parent, false);
            return new MYholder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            MYholder holder1 = (MYholder) holder;
            holder1.setdata(position);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        private class MYholder extends RecyclerView.ViewHolder {

            private final TextView tv_acction_name, tv_acction_number, tv_acction_beizhu, tv_acction_time;

            public MYholder(View view) {
                super(view);
                tv_acction_name = view.findViewById(R.id.tv_acction_name);
                tv_acction_number = view.findViewById(R.id.tv_acction_number);
                tv_acction_beizhu = view.findViewById(R.id.tv_acction_beizhu);
                tv_acction_time = view.findViewById(R.id.tv_acction_time);
            }

            public void setdata(final int position) {

                if (list.get(position).getState().equals("1")){ // state 1 增加 2 减少
                    tv_acction_number.setText("+"+list.get(position).getMoney());
                }else if (list.get(position).getState().equals("2")){
                    tv_acction_number.setText("-"+list.get(position).getMoney());
                }
                tv_acction_name.setText(list.get(position).getInfo());

                tv_acction_time.setText(list.get(position).getAdd_time());


            }
        }
    }
}
